#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 1e6 + 10, INF = 0x3f3f3f3f;

int a, b;
int pw[9] = { 1,10,100,1000,10000,100000,1000000,10000000,100000000 };
map<int, int> hs;

int R(int n, int i)
{
    if (i == 0) {
        int x = n / pw[8] % 10;
        int y = n / pw[5] % 10;
        int z = n / pw[2] % 10;
        n -= x * pw[8] + y * pw[5] + z * pw[2];
        n += z * pw[8] + x * pw[5] + y * pw[2];
    } else if (i == 1) {
        int x = n / pw[7] % 10;
        int y = n / pw[4] % 10;
        int z = n / pw[1] % 10;
        n -= x * pw[7] + y * pw[4] + z * pw[1];
        n += z * pw[7] + x * pw[4] + y * pw[1];
    } else if (i == 2) {
        int x = n / pw[6] % 10;
        int y = n / pw[3] % 10;
        int z = n / pw[0] % 10;
        n -= x * pw[6] + y * pw[3] + z * pw[0];
        n += z * pw[6] + x * pw[3] + y * pw[0];
    }
    return n;
}

int D(int n, int i)
{
    if (i == 0) {
        int x = n / pw[8] % 10;
        int y = n / pw[7] % 10;
        int z = n / pw[6] % 10;
        n -= x * pw[8] + y * pw[7] + z * pw[6];
        n += z * pw[8] + x * pw[7] + y * pw[6];
    } else if (i == 1) {
        int x = n / pw[5] % 10;
        int y = n / pw[4] % 10;
        int z = n / pw[3] % 10;
        n -= x * pw[5] + y * pw[4] + z * pw[3];
        n += z * pw[5] + x * pw[4] + y * pw[3];
    } else if (i == 2) {
        int x = n / pw[2] % 10;
        int y = n / pw[1] % 10;
        int z = n / pw[0] % 10;
        n -= x * pw[2] + y * pw[1] + z * pw[0];
        n += z * pw[2] + x * pw[1] + y * pw[0];
    }
    return n;
}

int S(int n)
{
    int tmp[10], tt = 10;
    while (n) {
        tmp[--tt] = n % 10;
        n /= 10;
    }
    int t = tmp[1];
    tmp[1] = tmp[3];
    tmp[3] = tmp[9];
    tmp[9] = tmp[7];
    tmp[7] = t;

    t = tmp[2];
    tmp[2] = tmp[6];
    tmp[6] = tmp[8];
    tmp[8] = tmp[4];
    tmp[4] = t;

    for (int i = 9;i >= 1;i--)
        n = n * 10 + tmp[i];
    return n;
}

void bfs()
{
    queue<int> q;
    q.push(123456789);
    while (!q.empty()) {
        int num = q.front(), nx;q.pop();
        int d = hs[num];
        auto push = [&](int x) {
            auto it = hs.find(x);
            if (it == hs.end()) {
                q.push(x);
                hs[x] = d + 1;
            }};

        for (int i = 0;i < 3;i++) {
            nx = R(num, i);
            push(nx);
            nx = D(num, i);
            push(nx);
        }
        nx = S(num); push(nx);
    }
}

void solve()
{
    map<int, int> s;
    for (int i = 1;i <= 9;i++) {
        char ch; cin >> ch;
        s[ch - '0'] = i;
    }
    int num = 0;
    for (int i = 1;i <= 9;i++) {
        char ch;cin >> ch;
        num = num * 10 + s[ch - '0'];
    }
    if (num == 123456789) cout << 0;
    else if (hs[num] == 0) cout << -1;
    else cout << hs[num];
    cout << '\n';
}

signed main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    bfs();
    int T = 1;
    cin >> T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}